package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PointPass;
import com.ruoyi.system.service.IPointPassService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 积分通Controller
 * 
 * @author ruoyi
 * @date 2022-10-26
 */
@RestController
@RequestMapping("/system/pass")
public class PointPassController extends BaseController
{
    @Autowired
    private IPointPassService pointPassService;

    /**
     * 查询积分通列表
     */
    @PreAuthorize("@ss.hasPermi('system:pass:list')")
    @GetMapping("/list")
    public TableDataInfo list(PointPass pointPass)
    {
        startPage();
        List<PointPass> list = pointPassService.selectPointPassList(pointPass);
        return getDataTable(list);
    }



    @PreAuthorize("@ss.hasPermi('system:pass:list')")
    @GetMapping("/getList")
    public AjaxResult getList(PointPass pointPass)
    {
        AjaxResult success = AjaxResult.success();
        List<PointPass> list = pointPassService.selectPointPassList(pointPass);
        ArrayList<PointPass> resultList = new ArrayList<>();
        for (PointPass pass : list) {
           if (pass.getPass()==1){
               resultList.add(pass);
           }
        }
        success.put("list",resultList);
        return success;
    }
    /**
     * 查询积分通列表
     */
    @PreAuthorize("@ss.hasPermi('system:pass:list')")
    @GetMapping("/selectTableData")
    public AjaxResult list()
    {
        AjaxResult success = AjaxResult.success();
        List<HashMap> list = pointPassService.selectTableData();
        ArrayList<String> addressList = new ArrayList<>();
        ArrayList<Integer> numList = new ArrayList<>();
        for (HashMap hashMap : list) {
            String address = hashMap.get("address").toString();
            Integer num = Integer.valueOf(hashMap.get("num").toString());
            addressList.add(address);
            numList.add(num);
        }
        success.put("addressList",addressList);
        success.put("numList",numList);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return success;
    }

    /**
     * 导出积分通列表
     */
    @PreAuthorize("@ss.hasPermi('system:pass:export')")
    @Log(title = "积分通", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PointPass pointPass)
    {
        List<PointPass> list = pointPassService.selectPointPassList(pointPass);
        ExcelUtil<PointPass> util = new ExcelUtil<PointPass>(PointPass.class);
        util.exportExcel(response, list, "积分通数据");
    }

    /**
     * 获取积分通详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:pass:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(pointPassService.selectPointPassById(id));
    }

    /**
     * 新增积分通
     */
    @PreAuthorize("@ss.hasPermi('system:pass:add')")
    @Log(title = "积分通", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PointPass pointPass)
    {
        return toAjax(pointPassService.insertPointPass(pointPass));
    }

    /**
     * 修改积分通
     */
    @PreAuthorize("@ss.hasPermi('system:pass:edit')")
    @Log(title = "积分通", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody PointPass pointPass)
    {
        return toAjax(pointPassService.updatePointPass(pointPass));
    }


    /**
     * 删除积分通
     */
    @PreAuthorize("@ss.hasPermi('system:pass:remove')")
    @Log(title = "积分通", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pointPassService.deletePointPassByIds(ids));
    }
}
